package com.hd.controller;

import com.hd.pojo.XInfo;
import com.hd.pojo.XPart;
import com.hd.service.XInfoService;
import com.hd.service.XPartService;
import com.hd.tools.Log;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * Created by 1 on 2017/11/29.
 */
@Controller
@RequestMapping("/director/xInfo")
public class XInfoController {

    @Resource
    private XPartService xPartService;

    @Resource
    private XInfoService xInfoService;

    private String msg;

    @RequestMapping("/searchXPosition")
    public String searchXPosition(Integer caseId, Model model){
        List<XPart> xPartList = xPartService.getAllXPart();
        model.addAttribute("caseId",caseId);
        model.addAttribute("xPartList",xPartList);
        return "director/xInfo";
    }

    @ResponseBody
    @PostMapping("/addXInfo")
    public String addXInfo(XInfo xInfo){
        xInfo.setxDateTime(LocalDateTime.now());
        try{
            xInfoService.addXInfo(xInfo);
            msg = "开X光检查成功!";
        }catch (RuntimeException ex){
            msg = "开X光检查失败!";
            Log.logger.error(msg,ex);
        }
        return msg;

    }

    @RequestMapping("/searchXInfo")
    public String searchXInfo(Model model){
        XInfo xInfo = xInfoService.searchXInfo();
        model.addAttribute("xInfo",xInfo);
        return "director/enterXInfo";
    }

    @ResponseBody
    @RequestMapping("/enterXInfo")
    public String enterXInfo(XInfo xInfo){
        xInfo.setxDateTime(LocalDateTime.now());
        try{
            xInfoService.enterXInfo(xInfo);
            msg = "录入成功!";
        }catch (RuntimeException ex){
            msg = "录入失败!";
            Log.logger.error(msg,ex);
        }
        return msg;
    }

    @ResponseBody
    @RequestMapping("/viewXInfo")
    public List<XInfo> viewXInfo(Integer caseId,String[] xPosition){
        List<XInfo> xInfoList = null;
        if(xPosition!=null &&xPosition.length>0) {
            xInfoList = xInfoService.searchXInfoByCaseId(caseId, xPosition);
        }
        return xInfoList;
    }

}
